package cn.fafu.fafu_record.config.Exception;

import cn.fafu.fafu_record.result.ResultEnum;
import cn.fafu.fafu_record.utils.ResultUtil;
import cn.fafu.fafu_record.result.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @description 全局异常处理
 */
@ControllerAdvice
public class GloablExceptionHandler extends RuntimeException {
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result handle(Exception e) {
        if (e instanceof MyException) {
            MyException myException = (MyException) e;
            return ResultUtil.error(myException.getCode(), myException.getMessage());
        } else {
            return ResultUtil.error(ResultEnum.UNKNOWN_ERROR.getCode(), ResultEnum.UNKNOWN_ERROR.getMsg());
        }
    }

}